home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / swags-z / sorting.swg / 0047_Sorting.pas < prev    next >
Pascal/Delphi Source File  |  1994-05-25  |  1KB  |  32 lines

  1. {
  2.  DR> Does anyone have a good routine to sort a string array into
  3.  DR> alphabetical order - I really only know how to do a bubble
  4.  DR> sort, and that's a bit slow for >1000 in the array...
  5.  DR> Preferably written in standard Pascal, as I would like to
  6.  DR> understand it,
  7.  
  8.    Here's the conventional QuickSort (which is also included in the full
  9. TP/BP packages as examples):
  10. }
  11.  
  12. var T     : string;                                  { swap variable }
  13.     GUESS : array[1..1000] of ^string;    { pointer array of strings }
  14. procedure L_HSORT (LEFT,RIGHT : word);             { Lo-Hi QuickSort }
  15. var LOWER,UPPER,MIDDLE : word;
  16.     PIVOT              : string;
  17. begin
  18.   LOWER := LEFT; UPPER := RIGHT; MIDDLE := (LEFT+RIGHT) div 2;
  19.   PIVOT := GUESS[MIDDLE]^;
  20.   repeat
  21.     while GUESS[LOWER]^ < PIVOT do Inc(LOWER);
  22.     while PIVOT < GUESS[UPPER]^ do Dec(UPPER);
  23.     if LOWER <= UPPER then
  24.       begin
  25.         T := GUESS[LOWER]^; GUESS[LOWER]^ := GUESS[UPPER]^;
  26.         GUESS[UPPER]^ := T; Inc (LOWER); Dec (UPPER);
  27.       end;
  28.   until LOWER > UPPER;
  29.   if LEFT < UPPER then L_HSORT (LEFT, UPPER);
  30.   if LOWER < RIGHT then L_HSORT (LOWER, RIGHT)
  31. end;                                                       { L_HSORT }
  32.